import ExamStatus from '@/components/type-of-exam/types/exam-status';
import { QuestDefaultApi } from '@/library/util/api';
import { defaultErrorHandler } from '@/library/util/error-handler';
import { message } from 'ant-design-vue';
import { defineStore } from 'pinia';
import QuestAdapter from '../design/adpater/QuestAdapter';
import { ExamItem, Questionaire } from '../design/types';
import { useLoginInforStore } from '../login';
import AnswerAdapter from './adpater/AnswerAdapter';
import { QuestionResult } from './types';
interface AnswerContentState {
  isLoading: boolean;

  questId: string;

  /** 问卷信息 */
  questionaire: Questionaire;

  /** 题目信息 */
  content: ExamItem[];

  ip: string;

  tips: string;
}
export const useAnswerContentStore = defineStore({
  id: 'AnswerContentStore',
  state: (): AnswerContentState => ({
    isLoading: false,
    questId: '',
    questionaire: new Questionaire(),
    content: [],
    ip: '',
    tips: '',
  }),
  getters: {
    /**
     * 获取题列表
     */
    getContentList(): ExamItem[] {
      // console.log('this.content', this.content);
      return this.content;
    },

    /**
     * 获取问卷信息
     * @returns
     */
    getQuestionaire(): Questionaire {
      return this.questionaire;
    },
  },
  actions: {
    /**
     * 获取问卷试题
     */
    async getContent() {
      try {
        if (!this.questId) {
          throw new Error('请刷新重试');
        }

        // const isAnswered = await QuestDefaultApi.answerIsAnsweredGet({
        //   queId: this.questId,
        // });

        // if (isAnswered.answered) {
        //   throw new Error('当前ip已作答');
        // }

        this.isLoading = true;
        const result =
          (await QuestDefaultApi.questionaireQuestionaireDetailsGet({
            queId: this.questId,
          })) as QuestionResult;

        if (result) {
          this.content = new QuestAdapter().composeDetails(
            result,
            ExamStatus.Answer,
          );

          this.questionaire = new QuestAdapter().compose(result);
        }
      } catch (error) {
        defaultErrorHandler(error);
      } finally {
        this.isLoading = false;
      }
    },

    /**
     * 设置问卷id
     * @param value
     */
    setQuestId(value: AnswerContentState['questId']) {
      this.questId = value;
    },

    /**
     * 上传答案
     */
    async anwser() {
      try {
        const { getUserInfo } = useLoginInforStore();
        const params = new AnswerAdapter().parse(
          this.questionaire,
          this.content,
          String(getUserInfo.userId),
        );

        await QuestDefaultApi.answerSubmitAnswerPost(params);

        message.success('作答成功！');
      } catch (error) {
        defaultErrorHandler(error);
      }
    },

    /**
     * 获取当前用户ip
     */
    async getIp() {
      try {
        const result = await QuestDefaultApi.answerGetIPAddressGet();

        if (result) {
          this.ip = result as unknown as string;
        }
      } catch (error) {
        defaultErrorHandler(error);
      }
    },

    async queryAnwser() {
      try {
        await this.getIp();

        await this.anwser();
      } catch (error) {
        defaultErrorHandler(error);
      }
    },

    reSetTips() {
      this.tips = '';
    },
  },
});
